package com.umgsai.springbootstudy.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Service;

import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

@Slf4j
@Service
public class TestService {

    @Async
    public void testAsync(){
        try {
            log.info("sleep开始，{}", Thread.currentThread().getName());
            TimeUnit.SECONDS.sleep(3);
            log.info("sleep结束，{}", Thread.currentThread().getName());
        } catch (Exception e){
            log.error("Exception", e);
        }
    }

    @Async
    public Future<String> testAsync(String name){
        try {
            log.info("sleep开始，threadName={}, name={}", Thread.currentThread().getName(), name);
            TimeUnit.SECONDS.sleep(3);
            log.info("sleep结束，threadName={}, name={}", Thread.currentThread().getName(), name);
        } catch (Exception e){
            log.error("Exception", e);
        }
        return new AsyncResult<>("Hello " + name);
    }
}
